package com.exam.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.demo.common.Constants;
import com.exam.demo.common.Result;
import com.exam.demo.controller.dto.UserDTO;
import com.exam.demo.entity.Admin;
import com.exam.demo.entity.Student;
import com.exam.demo.entity.Teacher;
import com.exam.demo.service.AdminService;
import com.exam.demo.service.StudentService;
import com.exam.demo.service.TeacherService;
import com.exam.demo.util.ObjectMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能描述
 *
 * @author yxj30
 * @date 2022/03/16  19:18
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    StudentService studentService;
    @Resource
    TeacherService teacherService;
    @Resource
    AdminService adminService;


    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO) {
        String userId = userDTO.getUserId();
        String password = userDTO.getPassword();
        if (StrUtil.isBlank(userId) || StrUtil.isBlank(password)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        //按id的长度分不同角色
        if(userId.length() == 13)  {
            //学生
            Student student = new Student();
            student.setStudentId(userId);
            student.setStudentPassword(password);
            UserDTO user = studentService.login(student);
            return Result.success(user);
        }else if(userId.length() == 10){
            //教师
            Teacher teacher = new Teacher();
            teacher.setTeacherId(userId);
            teacher.setTeacherPassword(password);
            UserDTO user = teacherService.login(teacher);
            return Result.success(user);
        }else if(userId.length() == 6){
            //管理员
            Admin admin = new Admin();
            admin.setAdminId(userId);
            admin.setAdminPassword(password);
            UserDTO user = adminService.login(admin);
            return Result.success(user);
        }
        return Result.error("401","账号错误");
    }




}
